package qhdx.fiveGC.controller;

import qhdx.fiveGC.entity.Product;
import qhdx.fiveGC.entity.User;
import qhdx.fiveGC.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;
    @RequestMapping("/product/add")
    public int add(Product product, MultipartFile picFile,
                   HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (user==null){
            return 2;//代表未登录
        }
        //保存图片
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //准备保存图片的文件夹路径
        String dirPath = "F:/upload/";
        //得到日期相关的路径  F:/upload/2021/11/22/xxxx.jpg
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd/");
        //new Date得到的是表示当前系统时间的对象
        //datePath = 2021/11/22/
        String datePath = f.format(new Date());
        System.out.println("日期路径:"+datePath);
        //F:/upload/2021/11/22/
        dirPath = dirPath+datePath;
        File dirFile = new File(dirPath);
        //判断文件夹是否存在
        if (!dirFile.exists()){
            dirFile.mkdirs();// 创建文件夹
        }
        picFile.transferTo(new File(dirPath+fileName));//保存图片

        //把数据保存到数据库中    /xxxx.jpg
        product.setUrl("/"+datePath+fileName);
        product.setCreated(new Date()); //保存当前的时间
        mapper.insert(product);
        return 1;
    }

    @RequestMapping("/product/select")
    public List<Product> select(){
        return mapper.select();
    }
    @RequestMapping("/product/select/home")
    public List<Product> selectForHome(){
        return mapper.selectForHome();
    }

    @RequestMapping("/product/delete")
    public void delete(int id){

        //得到图片的url
        String url = mapper.selectUrlById(id);
        //得到图片的完整路径 F:/upload/2021/11/22/xxxx.jpg
        String filePath = "F:/upload"+url;
        new File((filePath)).delete();//删除文件


        mapper.delete(id);
    }

    @RequestMapping("/product/selectView")
    public List<Product> selectView(){
        return mapper.selectView();
    }
    @RequestMapping("/product/selectLike")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }

    @RequestMapping("/product/selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }

    @RequestMapping("/product/search")
    public List<Product> search(String wd){
        return mapper.selectByWd(wd);
    }

    @RequestMapping("/product/selectById")
    public Product selectById(int id,HttpSession session){
        String str = (String) session.getAttribute("view"+id);
        if (str==null){
            //让浏览量+1
            mapper.viewById(id);
            session.setAttribute("view"+id,"viewed");
        }
        return mapper.selectById(id);
    }

    @RequestMapping("/product/like")
    public int like(int id,HttpSession session){
        String str = (String) session.getAttribute("like"+id);
        if (str==null){//代表没有点过赞
            mapper.likeById(id);
            //把点过赞的id保存到session中
            session.setAttribute("like"+id,"liked");
            return 1;//代表点赞成功
        }
        return 2;//代表已经点过赞
    }
}
